<?php
require_once dirname(dirname(__FILE__)) . '/shared/config/config.php';
require_once DOCUMENT_ROOT . DIR_ADMIN_INCLUDE . 'check_user_login.php';
?>
<!DOCTYPE html>
<html>
    <head id="Head1">
        <title>
            Manage Articles - Tesdo Admin
        </title>
        <?php
        
        require_once DOCUMENT_ROOT . DIR_ADMIN_INCLUDE . 'head.php';
        require_once DOCUMENT_ROOT . DIR_ADMIN_INCLUDE . 'header.php';
        require_once DOCUMENT_ROOT . DIR_ADMIN_INCLUDE . 'script.php';
        require_once DOCUMENT_ROOT . DIR_ADMIN_INCLUDE . 'ckeditor.php';
        require_once DOCUMENT_ROOT . DIR_SHARED_DAO . 'dao_journal.php';
        require_once DOCUMENT_ROOT . DIR_SHARED_DAO . 'dao_category.php';
        require_once DOCUMENT_ROOT . DIR_SHARED_DAO . 'dao_journal_sub_detail.php';
        require_once DOCUMENT_ROOT . DIR_SHARED_LIBRARIES . 'lib_pager.php';
        require_once DOCUMENT_ROOT . DIR_SHARED_LIBRARIES . 'lib_upload.php';
        require_once DOCUMENT_ROOT . DIR_SHARED_LIBRARIES . 'lib_data_input.php';
        ?>
        <script type="text/javascript">
            function call() {
                document.getElementById('hiddenMsg').value = editor.getData();
            }
            function my_confirm(id) {
                if (confirm('Are you sure?')) {
                    set_value('delete_id', id);
                    submit_form('form_articles_delete');
                } else {
                    return false;
                }
            }
        </script>
    </head>
    <body>
        <?php
        //CLEAR & CANCEL
        if (!empty($_POST['btnClear']) || !empty($_POST['btnCancel'])) {
            ?>
            <script type="text/javascript">
                window.location = <?php echo LINK_ROOT . DIR_ADMIN . 'working_papers.php'; ?>;
            </script>
        <?php
        }
        
        $journal_access = new dao_journal();
        $category_access = new dao_category();
        $journal_sub_detail_access = new dao_journal_sub_detail();
        $lib_pager = new lib_pager();

        if (isset($_POST['btnSave']) && $_POST['btnSave'] == 'Save') {
            if ((!isset($_POST['ddlJournals']) ||
                    !isset($_POST['ddlCategory']) ||
                    !isset($_POST['txtDescription']) ||
                    $_POST['ddlJournals'] == '' ||
                    $_POST['ddlCategory'] == '' ||
                    $_POST['txtDescription'] == '')) {
                ?>
                <script type="text/javascript">
                    alert('You need to fulfill the information!');
                </script> 
                <?php
            } else {
                $journals_sub_id = $_POST['hiddJournalSubDetaitID'];
                $description = $_POST['txtDescription'];
                $journal_id = $_POST['ddlJournals'];
                $category_id = $_POST['ddlCategory'];
                if ($journal_sub_detail_access->save($journals_sub_id, $description, $journal_id, $category_id) == 1) {
                    ?>
                    <script type="text/javascript">
                        alert('<?php if ($journals_sub_id==0) { echo "Added"; } else { echo "Updated"; }?> articles successfully!');
                        window.location = 'articles.php';
                    </script> 
                <?php } else { ?>
                    <script type="text/javascript">
                        alert('Failed to <?php if ($journals_sub_id==0) { echo "add"; } else { echo "update"; }?> articles!');
                    </script> 
                    <?php
                }
            }
        }
        //Check type and set $type value
        if (isset($_GET['t'])) {
            $t = $_GET['t'];
            if ($t == 'e')
                $type = 'edit';
            else
                $type = 'delete';
        }

        //Get ID
        $id = 0;
        if (isset($_GET['id'])) {
            $id = $_GET['id'];
        }
        
        //Edit
        if (isset($type) && $type == 'edit') {
            $edit_article_old = $journal_sub_detail_access->get_by_id($id);
        }


        //Delete
        if (!empty($_POST["delete_id"])) {
            
            $journals_sub_id = $_POST["delete_id"];
            if ($journal_sub_detail_access->delete($journals_sub_id)) {
                ?>
                <script type="text/javascript">
                    alert('Delete articles successfully!');
                    window.location = 'articles.php';
                </script> 
            <?php } else { ?>
                <script type="text/javascript">
                    alert('Failed to delete articles!');
                </script> 
            <?php
            
            }
        }
        ?>
        <div>
            <div class="content-box-outer">
                <div class="content-box-right">
                </div>
                <div class="content-box-left">
                    <form action="articles.php" method="POST" id="form_articles_delete" >
                        <input type="hidden" name="delete_id" value="<?php echo $id; ?>" />
                    </form>
                    <form action="articles.php" method="POST" id="form_articles" onsubmit="call()" >
                        <h1>Journals Sub Detail</h1>
                        <input type="hidden" name="hiddJournalSubDetaitID" value="<?php echo $id; ?>" />
                        
                        <div class="TitleArea"><b>Select Journal: </b></div>
                        <div class="textBoxArea">
                            <select name="ddlJournals" id="ddlJournals" style="width:300px;">
                                <?php
                                $list = $journal_access->get_all();
                                
                                
                                foreach ($list as $i => $item) {
                                    ?>
                                    <option value="<?php echo $item['JournalID'] ?>"
                                    <?php
                                    if (!empty($edit_article_old)) {

                                        if ($item['JournalID'] == $edit_article_old['JournalID']) {
                                            echo 'selected';
                                        }
                                    }
                                    ?>
                                            >
                                        <?php echo $item['Title'] ?> </option>
                                <?php
                                
                                }
                                $journal_list = $list;
                                $journal_id = 0;
                                
                                if (!empty($edit_article_old['JournalID'])) {
                                    $journal_id = $edit_article_old['JournalID'];
                                } else if (!empty($journal_list) && sizeof($journal_list)) {
                                    $journal_id = $journal_list[0]['JournalID'];
                                }
                                ?>
                            </select>
                            <script type="text/javascript">
                                function filldata(value) {
                                    $.ajax({
                                        type: "POST",
                                        url: "<?php echo LINK_ROOT . DIR_ADMIN_INCLUDE; ?>api_get_category_options_by_journal_id.php",
                                        data: "journal_id=" + value,
                                        success: function(result) {
                                            $("select[name=ddlCategory]").html(result);
                                        }
                                    });
                                }
                                $( "#ddlJournals" ).change(function() {
                                    filldata($( this ).val());
                                });
                                filldata('<?php echo $journal_id; ?>');
                            </script>
                        </div>
                        <div class="TitleArea"><b>Select Category: </b></div>
                        <div class="textBoxArea">
                            <select name="ddlCategory" id="ddlCategory" style="width:300px;">
                                
                            </select>
                        </div>
                        <br>
                        <div class="TitleArea"><b>Description: </b></div>
                        <div style="margin-top: 100px;">
                            <input type="hidden" name="hiddenMsg" value="">
                            <textarea class="ckeditor" id="txtDescription" name="txtDescription">
                            <?php
                            if (!empty($edit_article_old['Description'])) {
                                echo $edit_article_old['Description'];
                            }
                            ?>
                            </textarea>
                        </div>
                        <div class="textBoxArea" style="margin-bottom: 30px;">
                            <input type="submit" name="btnSave" value="Save" id="btnSave" class="buttonDesign">
                            <input type="submit" name="btnClear" value="Clear" id="btnClear" class="buttonDesign">
                        </div>
                    </form>
                    <br clear="all">
                    <h1>Manage Journals Sub Detail</h1>
                    <div>
                        <table cellspacing="0" cellpadding="4" rules="cols" id="cphAdmin_gvJournalSubDetail" style="color:Black;background-color:White;border-color:#DEDFDE;border-width:1px;border-style:None;width:988px;border-collapse:collapse;">
                            <tbody>
                                <tr style="color:White;background-color:#6B696B;font-weight:bold;">
                                    <th scope="col">Journal Title</th><th scope="col">Category</th><th scope="col">Description</th><th scope="col">Actions</th>
                                </tr>
                                <?php

                                $current_page = 1;
                                $page_size = 10;
                                if (!empty($_GET['p'])) {
                                    $current_page = $_GET['p'];
                                }
                                
                                $list_total = $journal_sub_detail_access->get_all();
                                $total_page = $lib_pager->get_total_page($list_total, $page_size);
                                $current_page_list = $lib_pager->get_current_page_list($page_size, $current_page, $list_total);

                                $count = 0;
                                foreach ($current_page_list as $i => $item) {
                                    if ($count % 2 == 0) {
                                        ?>
                                        <tr style="background-color:#F7F7DE;">
                                    <?php } else { ?>
                                        <tr style="background-color:#FFFFFF;">
                                    <?php } ?>
                                            <td><?php echo $item['Title']; ?></td>
                                            <td style="width:200px;"><?php echo $item['catTitle']; ?></td>
                                            <td><?php echo substr(strip_tags($item['Description']), 0, 200); ?></td>
                                            <td align="center">
                                                <a href="articles.php?t=e&id=<?php echo $item['JournalsSubID'] ?>">
                                                    <img src="<?php echo LINK_ROOT.DIR_ADMIN_LAYOUT_IMAGE; ?>/edit.png" style="width:15px; outline: none;" />
                                                </a>
                                                <input type="image" 
                                                       name="ImageButtonDelete<?php echo $count; ?>" 
                                                       id="ImageButtonDelete<?php echo $count; ?>" 
                                                       title="Delete" 
                                                       src="<?php echo LINK_ROOT.DIR_ADMIN_LAYOUT_IMAGE; ?>/delete.png" 
                                                       onclick="return my_confirm(<?php echo $item['JournalsSubID'] ?>);" style="width:15px;">
                                            </td>
                                        </tr>
                                        <?php
                                    
                                    $count++;
                                }
                                ?>
                                <tr align="right" style="color:Black;background-color:#F7F7DE;">
                                    <td colspan="4">
                                        <table>
                                            <tbody>
                                                <tr align="right" style="color:Black;background-color:#F7F7DE;">
                                                    <td colspan="4">
                                                        <table>
                                                            <tbody>
                                                                <tr>
                                                                    <?php for ($a = 1; $a <= $total_page; $a++) { ?>
                                                                        <td>
                                                                            <span>
                                                                                <a href="articles.php?p=<?php echo $a; ?>" <?php if ($current_page != $a) echo 'style="color:Black;"'; ?>><?php echo $a; ?></a>
                                                                            </span>
                                                                        </td>
                                                                    <?php } ?>
                                                                </tr>
                                                            </tbody>
                                                        </table>
                                                    </td>
                                                </tr>
                                            </tbody>
                                        </table>
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
               
                </div>
                
                <div class="MessageArea">
                    <span id="cphAdmin_lblMsg"></span>
                </div>
            </div>

        </div>

        <?php
        require_once DOCUMENT_ROOT . DIR_ADMIN_INCLUDE . 'footer.php';
        ?>
    </body>
</html>
